﻿using System;
using System.Data;
using Core.Library;
using DataObject;

namespace Event.Controls.News
{
    public partial class List : System.Web.UI.UserControl
    {
        #region variables, constants, properties
        private const int _imageWidth = 150;
        private const int _imageHeight = 95;
        private const string LI_FORMAT = @"<li class=""{7}"">{3}<h3><a title=""{1}"" href=""{2}"">{0}</a></h3><h5>by <b>{4}</b> / <i>{5}</i></h5><p>{6}</p></li>";
        #endregion

        internal bool DataBind(DataTable dt)
        {
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    var cls = string.Empty;
                    var r = dt.Rows[i];
                    var avatar = Utils.Photo.BuildPhoto(
                    source: r[Constants.Table_News.News_Image].ToString(),
                    title: r[Constants.Table_News.News_Title].ToString(),
                    href: r[Constants.Table_News.Url].ToString(),
                    width: _imageWidth,
                    height: _imageHeight,
                    mustThumb: true);
                    if (i == (dt.Rows.Count - 1)) cls = "last";

                    this.ltrList.Text += string.Format(LI_FORMAT,
                    r[Constants.Table_News.News_Title],
                    r[Constants.Table_News.EncodeTitle],
                    r[Constants.Table_News.Url],
                    avatar,
                    r[Constants.Table_News.News_PenName],
                    r[Constants.Table_News.DisplayPublishDate],
                    r[Constants.Table_News.News_Sapo],
                    cls);
                }
                return true;
            }
            return false;
        }
    }
}